#!/bin/sh

#  Copyright (C) 2014-2018, 2021 D. R. Commander.  All Rights Reserved.
#  Copyright (C) 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
#  Copyright (C) 2002 Constantin Kaplinsky.  All Rights Reserved.
#
#  This is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This software is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this software; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
#  USA.

unset SESSION_MANAGER
DBUS_LAUNCH=
if [ -x /usr/bin/dbus-launch ]; then
  DBUS_LAUNCH=/usr/bin/dbus-launch
elif [ -x /usr/local/bin/dbus-launch ]; then
  DBUS_LAUNCH=/usr/local/bin/dbus-launch
fi
if [ "$DBUS_LAUNCH" != "" ]; then
  eval `$DBUS_LAUNCH --sh-syntax --exit-with-session`
  export DBUS_SESSION_BUS_ADDRESS
  echo "xstartup.turbovnc: Creating new session bus instance:"
  echo "xstartup.turbovnc:   $DBUS_SESSION_BUS_ADDRESS"
else
  unset DBUS_SESSION_BUS_ADDRESS
fi
unset DBUS_LAUNCH
XDG_SESSION_TYPE=x11;  export XDG_SESSION_TYPE

OS=`uname -s`

# Emulate GDM

# Use Unity 2D on Ubuntu 12 if no WM is specified.  Unity 3D 5.20.x doesn't
# even pretend to work properly with our X server.
if [ -f /usr/share/gnome-session/sessions/ubuntu-2d.session -a -z "$TVNC_WM" ]; then
  TVNC_WM=2d
fi
XSESSIONSDIR=
if [ -d /usr/share/xsessions ]; then
  XSESSIONSDIR=/usr/share/xsessions
elif [ -d /usr/local/share/xsessions ]; then
  XSESSIONSDIR=/usr/local/share/xsessions
fi
GREP=grep
if [ -x /usr/bin/ggrep ]; then
  GREP=ggrep
fi
SED=sed
if [ -x /usr/bin/gsed ]; then
  SED=gsed
fi
NOXSESSION=0
case "$TVNC_WM" in
  2d | 2d:noxsession)
    # TODO: Automate this somehow
    if [ "$TVNC_WM" = "2d:noxsession" ]; then
      NOXSESSION=1
      TVNC_WM=`echo $TVNC_WM | $SED -r 's/:noxsession$//g'`
    fi
    # RHEL 7, Fedora: gnome-classic
    # Ubuntu 12: ubuntu-2d
    # Ubuntu 14: gnome-fallback
    # Ubuntu 16+: gnome-flashback-metacity
    for SESSION in "gnome-classic" "gnome-fallback" "ubuntu-2d" "2d-gnome" \
      "gnome-flashback-metacity"; do
      if [ "$XSESSIONSDIR" != "" -a -f $XSESSIONSDIR/$SESSION.desktop ]; then
        DESKTOP_SESSION=$SESSION;  export DESKTOP_SESSION
      fi
    done
    unset TVNC_WM
    ;;
  *)
    # TODO: Automate this somehow
    case "$TVNC_WM" in
      *:noxsession)
        NOXSESSION=1
        TVNC_WM=`echo $TVNC_WM | $SED -r 's/:noxsession$//g'`
        ;;
      *)
        ;;
    esac
    if [ "$TVNC_WM" = "" ]; then
      for SESSION in "gnome" "ubuntu"; do
        if [ "$XSESSIONSDIR" != "" -a -f $XSESSIONSDIR/$SESSION.desktop ]; then
          DESKTOP_SESSION=$SESSION;  export DESKTOP_SESSION
        fi
      done
    else
      # For backward compatibility with TurboVNC 2.2.x, we allow a window
      # manager's startup script (e.g. mate-session, gnome-session) to be
      # specified using -wm / $wm, but if there is a matching session desktop
      # file for it, we use that instead.
      SESSION=`echo $TVNC_WM | $SED -r 's/^.*\/|-session$//g'`
      if [ "$XSESSIONSDIR" != "" -a -f "$XSESSIONSDIR/$SESSION.desktop" ]; then
        DESKTOP_SESSION=$SESSION;  export DESKTOP_SESSION
        unset TVNC_WM
      fi
      unset SESSION
    fi
    ;;
esac
XSESSION=
if [ "$DESKTOP_SESSION" != "" ]; then
  GDMSESSION=$DESKTOP_SESSION;  export GDMSESSION
  XDG_SESSION_DESKTOP=$DESKTOP_SESSION;  export XDG_SESSION_DESKTOP
  echo "xstartup.turbovnc: Using '$DESKTOP_SESSION' window manager in"
  echo "xstartup.turbovnc:   $XSESSIONSDIR/$DESKTOP_SESSION.desktop"
  if $GREP -qE "^Exec\s*=" $XSESSIONSDIR/$DESKTOP_SESSION.desktop; then
    TVNC_WM=`$GREP -E "^Exec\s*=" $XSESSIONSDIR/$DESKTOP_SESSION.desktop | $SED -r 's/^[^=]+=[[:space:]]*//g'`
    if [ "$NOXSESSION" != "1" ]; then
      if [ -x /etc/X11/xinit/Xsession ]; then
        XSESSION=/etc/X11/xinit/Xsession
      elif [ -x /etc/X11/Xsession ]; then
        XSESSION=/etc/X11/Xsession
      fi
    fi
  fi
  for KEY in DesktopNames X-LightDM-DesktopName; do
    if $GREP -qE "^$KEY\s*=" $XSESSIONSDIR/$DESKTOP_SESSION.desktop; then
      XDG_CURRENT_DESKTOP=`$GREP -E "^$KEY\s*=" $XSESSIONSDIR/$DESKTOP_SESSION.desktop | $SED -r 's/(^[^=]+=[[:space:]]*|;$)//g' | $SED -r 's/;/:/g'`
      export XDG_CURRENT_DESKTOP
    fi
  done
fi

if [ "$TVNC_VGL" = "1" ]; then
  if [ -z "$SSH_AGENT_PID" -a -x /usr/bin/ssh-agent ]; then
    TVNC_SSHAGENT=/usr/bin/ssh-agent
  fi
  if [ -z "$TVNC_VGLRUN" ]; then
    TVNC_VGLRUN="vglrun +wm"
  fi
fi

if [ "$TVNC_WM" = "" ]; then
  # Typical system-wide WM startup script on Linux and Solaris 11
  if [ -f /etc/X11/xinit/xinitrc ]; then
    TVNC_WM=/etc/X11/xinit/xinitrc
  fi
  # Typical system-wide WM startup script on Solaris 10
  if [ $OS = 'SunOS' -a -f /usr/dt/config/Xinitrc.jds ]; then
    TVNC_WM=/usr/dt/config/Xinitrc.jds
  fi
  # Typical per-user WM startup script on Solaris 10
  if [ $OS = 'SunOS' -a -f $HOME/.dt/sessions/lastsession ]; then
    TVNC_WM=`cat $HOME/.dt/sessions/lastsession`
  fi
fi
if [ "$TVNC_WM" != "" ]; then
  if [ "$XSESSION" != "" ]; then
    echo xstartup.turbovnc: Executing $TVNC_SSHAGENT $TVNC_VGLRUN $XSESSION \"$TVNC_WM\"
    exec $TVNC_SSHAGENT $TVNC_VGLRUN $XSESSION "$TVNC_WM"
  else
    echo xstartup.turbovnc: Executing $TVNC_SSHAGENT $TVNC_VGLRUN $TVNC_WM
    exec $TVNC_SSHAGENT $TVNC_VGLRUN $TVNC_WM
  fi
else
  echo "xstartup.turbovnc: No window manager specified or found.  Falling back to TWM."
  which twm >/dev/null && {
    if [ -f $HOME/.Xresources ]; then xrdb $HOME/.Xresources; fi
    xsetroot -solid grey
    xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
    twm
  } || {
    echo "xstartup.turbovnc: TWM not found.  I give up."
    exit 1
  }
fi
